import React,{ Component } from 'react';
import { List, Button, message } from 'antd';
import {reqSiginAskList,reqIsAgreeSigin} from '../../api';
import { connect } from 'react-redux';


class CheckSiginForm extends Component {
  state = {
    signinAskList:[],
    loading: false
  };

  getSiginAskList = async() => {   // 获取未处理的注册请求
    const org_id = this.props.user.org.org_id;
    this.setState({loading:true});
    const result = await reqSiginAskList(org_id);
    this.setState({loading:false});
    if(result.code === 200){
      const signinAskList = result.data;
      this.setState({
        signinAskList
      })
    }else{
      message.error(result.data);
    } 
  }
  
  isAgreeSignin = async(item,isPass) => {   // 是否同意注册   vvvvvvvvvvvvvv
    const user_mobile = item.user_mobile; 
    const org_id = item.org.org_id;
    const user_passed = isPass;
    this.setState({loading:true});
    const result = await reqIsAgreeSigin(user_mobile,org_id,user_passed);
    this.setState({loading:false});
    console.log("是否审批成功",result);
    if(result.code === 200){
      this.getSiginAskList();
    }else{
      // message.error("审批异常！");
      message.error(result.data);
    } 
  };


  componentDidMount(){
    this.getSiginAskList();
  }
  render() {
    const {loading,signinAskList} = this.state;
    return (
      <List
        className="check-list"
        dataSource={signinAskList}  
        loading = {loading}
        renderItem={item => (
          <List.Item className="check-signin-list-item">
            <div className="show-userInfo">
              <span className="sign-span">{item.user_name}发起注册申请</span>
              <p className="sign-p">手机号码：{item.user_mobile}</p>
              <p className="sign-p">组织名称：{item.org.org_name}</p>
            </div>  
            <div className="show-button">
              <Button type="primary" className="check-signin-button" onClick={() => this.isAgreeSignin(item,true)}>同意</Button>
              <Button className="check-signin-button" onClick={() => this.isAgreeSignin(item,false)}>拒绝</Button>
            </div>
          </List.Item>
        )}
      />
    )
  }
}

export default connect(
  state => ({user: state.user}),
  {}
)(CheckSiginForm);